﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IdSharp.AudioInfo;
using IdSharp.Common.Utils;
using IdSharp.Tagging.ID3v1;
using IdSharp.Tagging.ID3v2;
using IdSharp.Tagging.VorbisComment;
using IdSharp.Tagging.ID3v2.Frames;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Shoozla.DataModel
{
    public class IDSharpProcessor : IMP3Processor
    {
        public Mp3File ReadID3Tag(FileInfo song)
        {
            if (!song.Exists)
            {
                Trace.WriteLine("[ERROR] ReadID3Tag: File not found: " + song);
                return null;
            }

            Mp3File mp3 = null;

            try
            {
               // IAudioFile audioFile = AudioFile.Create(song.FullName, true);

              //  if (audioFile.FileType != AudioFileType.Mpeg)
              //      return null; //not MP3


                if (ID3v2Tag.DoesTagExist(song.FullName))
                {
                    IID3v2Tag id3v2 = new ID3v2Tag(song.FullName);
                    mp3 = new Mp3File(song);
                    mp3.Artist = id3v2.Artist;
                    mp3.Title = id3v2.Title;
                    mp3.AlbumName = id3v2.Album;
                    mp3.Genre = id3v2.Genre;
                    mp3.Year = id3v2.Year;
                    if (id3v2.PictureList.Count > 0)
                        mp3.Cover = id3v2.PictureList[0].Picture;

                    //Trace.WriteLine(String.Format("[INFO] ReadID3Tag: ID3v2: {0}", mp3));
                }
                else
                {
                    //fall back to v1
                    if (ID3v1Tag.DoesTagExist(song.FullName))
                    {
                        IID3v1Tag id3v1 = new ID3v1Tag(song.FullName);                        
                        mp3 = new Mp3File(song);
                        mp3.Artist = id3v1.Artist;
                        mp3.Title = id3v1.Title;
                        mp3.AlbumName = id3v1.Album;
                        //mp3.Genre = id3v1.genre //info not available, it is an integer
                        mp3.Year = id3v1.Year;
                        //Trace.WriteLine(String.Format("[INFO] ReadID3Tag: ID3v1: {0}", mp3));
                    }
                    else
                        Trace.WriteLine(String.Format("[INFO] ReadID3Tag: {0} : no mp3 TAG", song));
                }
            }
            catch (Exception AudioException)
            {
                Trace.WriteLine("[ERROR] ReadID3Tag: Exception: " + AudioException.Message);
                mp3 = null;
            }
            finally
            {
                if (mp3 != null)
                    mp3.Dirty = false;
            }

            return mp3;

        }

        public bool SaveID3Tag(Mp3File song)
        {
            try
            {
               // IAudioFile audioFile = AudioFile.Create(song.FileName.FullName, true);
                IID3v2Tag id3v2 = null;

                if (!ID3v2Tag.DoesTagExist(song.FileName.FullName))
                    id3v2 = new ID3v2Tag();
                else
                    id3v2 = new ID3v2Tag(song.FileName.FullName); //keep all existings tags

                id3v2.Year = song.Year;
                id3v2.Album = song.AlbumName;
                id3v2.Genre = song.Genre;
                id3v2.Title = song.Title;
                id3v2.Artist = song.Artist;

                //save only the Cover and delete all other pictures;
                if (song.HasCover)
                {
                    id3v2.PictureList.Clear();
                    IAttachedPicture cover = id3v2.PictureList.AddNew();

                    ////avoiding GDI errors
                    //Bitmap im = new Bitmap((Image)song.Cover);
                    //MemoryStream memCopy = new MemoryStream();
                    //im.Save(memCopy, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //cover.Picture = new Bitmap(memCopy);
                    //cover.PictureType = PictureType.CoverFront; // optional

                    using (MemoryStream stream = new MemoryStream())
                    {
                        Bitmap im = new Bitmap((Image)song.Cover);
                        im.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                        cover.Picture = Image.FromStream(stream);
                        cover.PictureType = PictureType.CoverFront; // optional
                    }
                     //SLOW!!
                }
  
                id3v2.Save(song.FileName.FullName);

                return true;
            }
            catch (Exception SaveID3TagEx)
            {
                Trace.WriteLine("[ERROR] SaveID3Tag: Exception: " + SaveID3TagEx.Message);
                return false;
            }
        }
    }
}
